package com.example.tass.controller;

import cn.tass.SRJ1933.devices.inf.TassSignServerApi;
import cn.tass.SRJ1933.devices.params.response.random.GenRandResponse;
import cn.tass.exceptions.TAException;
import com.example.tass.common.ResultMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Random_3_8 {
    private static final Logger logger = LoggerFactory.getLogger(SymEncAndDecTest_3_5.class);
    private static String printFormat = "错误码[%d]\n错误消息[%s]\n接口结果： %s\n";
    //private static TassSignServerApi signServerApi;
    //public Random_3_8() {
    //    signServerApi = InitServer_3_1.getSignServerApi();
    //}
    
    private TassSignServerApi signServerApi;
    public Random_3_8(TassSignServerApi signServerApi) {
        this.signServerApi = signServerApi;
    }

    @GetMapping(value = "/genRand")
    public ResultMessage genRand(@RequestParam int size){
        try {
            GenRandResponse randResponse = signServerApi.genRand(size);
            if(randResponse.getCode() != 0){
                logger.error(randResponse.getMsg());
                return ResultMessage.newFailureResult(randResponse.getMsg());
            }else {
                System.out.println("生成随机数： " + String.format(printFormat, randResponse.getCode(), randResponse.getMsg(), randResponse.getRand()));
                return ResultMessage.newSuccessResult(randResponse.getRand());
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }
}
